clear all
cap close
set more off

cd ".\Data_Program"

* This code generates the regression results in Tables 1, 2, 4 and 5 

use county,clear

* merge with division data
merge m:1 county using Econdata\countytodivision
keep if _merge==3 
drop _merge

* merge with neighbor data
merge 1:1 quarter county using sci_weighted_iv.dta
keep if _merge==3
drop _merge

* merge with Mintel data (proprietary)
merge 1:1 county quarter using Proprietary\mintel
drop if _merge==2 
drop _merge 
replace finad=0 if finad==.
replace nonfinad=0 if nonfinad==.
replace ad=0 if ad==.
gen finadshare = finad/ad

* merge with own economic data

    * CoreLogic house price growth (proprietary)
merge 1:1 quarter county using Proprietary\hpi_county_quarter_exp
drop if _merge==2
drop _merge
drop qhpi
replace hpgr=hpgr4

    * Census Buearu population growth
gen year=yofd(dofq(quarter))	
merge m:1 year county using Econdata\pop_county
drop if _merge==2
drop _merge
drop fracm frach

    * QCEW employment growth
merge m:1 year county using Econdata\emp_county_year
drop if _merge==2
drop _merge

    * CCP subprime share
merge 1:1 quarter county using Proprietary\subprime_county_quarter
drop if _merge==2  
drop _merge 

     * Census education level in 2010
merge m:1 county using Econdata\edu
drop if _merge==2  
drop _merge 

* demographic X year controls
tab year, gen(yy)
foreach i of numlist 1/10{
    gen baboveyy`i' = babove*yy`i'
	gen babove2yy`i' = babove2*yy`i'
	gen babove2_inXyy`i' = babove2_inX*yy`i'
	gen fracbyy`i' = fracb*yy`i'
	gen fracyyy`i' = fracy*yy`i'
}


* IRS migration flows
merge m:1 year county using Econdata\migration
drop if _merge==2  
drop _merge


* Regressions
xtset county quarter

* weights
sort county quarter
by  county: egen w1 = mean(tot)
by  county: egen w2 = mean(pop)
gen w=w1

scalar h = 4

  * outcome var
foreach x of varlist finshare share_refi finshare_refi nonbshare_refi nbnfshare_refi bankshare_refi finadshare{
gen	d`x'=`x'-l`=h'.`x'
gen	d`x'2=`x'2-l`=h'.`x'2
gen	d`x'2_outcz=`x'2_outcz-l`=h'.`x'2_outcz
}

gen dfinshare_pur =finshare_pur-l`=h'.finshare_pur
gen dfin=(fin-l`=h'.fin)/l`=h'.tot
gen dfin_refi=(finrefi-l`=h'.finrefi)/l`=h'.tot
gen dfin_pur=(finpur-l`=h'.finpur)/l`=h'.tot
gen dnonfin=((tot-fin)-(l`=h'.tot-l`=h'.fin))/l`=h'.tot
gen dtot = tot/l`=h'.tot-1

gen dlogfin = log(fin+1)-log(l`=h'.fin+1)
gen dlogfinrefi = log(finrefi+1)-log(l`=h'.finrefi+1)
gen dlogfinpur = log(finpur+1)-log(l`=h'.finpur+1)

gen dfinad = (log(finad+1)-log(l`=h'.finad+1))/100
gen dfinad2 = (log(finad2+1)-log(l`=h'.finad2+1))/100
gen dfinad2_outcz = (log(finad2_outcz+1)-log(l`=h'.finad2_outcz+1))/100
gen dfinad2_inX = (log(finad2_inX+1)-log(l`=h'.finad2_inX+1))/100

gen dfinshare2_outst=finshare2_outst-l`=h'.finshare2_outst  
gen dfinshare2_outdis100=finshare2_outdis100-l`=h'.finshare2_outdis100 
gen dfinshare2_outdis200=finshare2_outdis200-l`=h'.finshare2_outdis200 
gen dfinshare2_outcz_out10 = finshare2_outcz_out10-l`=h'.finshare2_outcz_out10
gen dfinshare2_outcz_out30 = finshare2_outcz_out30-l`=h'.finshare2_outcz_out30
gen dfinshare2_out10 = finshare2_out10-l`=h'.finshare2_out10
gen dfinshare2_out30 = finshare2_out30-l`=h'.finshare2_out30

gen dfinshare2_outcz_inX = finshare2_outcz_inX-l`=h'.finshare2_outcz_inX
gen dfinshare2_inX = finshare2_inX-l`=h'.finshare2_inX

global control1 hpgr popgr empgr fracyyy* fracbyy* subprime baboveyy* dfinad linflow loutflow
global control2 hpgr popgr empgr fracyyy* fracbyy* subprime baboveyy* dfinad linflow loutflow hpgr2 popgr2 empgr2 fracy2 fracb2 subprime2 babove2yy* dfinad2 
global control3 hpgr popgr empgr fracyyy* fracbyy* subprime baboveyy* linflow loutflow hpgr2 popgr2 empgr2 fracy2 fracb2 subprime2 babove2yy* 

save beforereg_sci_iv,replace



*===============================================================================
* Table 1. Baseline estimates
*===============================================================================

use beforereg_sci_iv,clear 

* Col 1
reghdfe dfinshare dfinshare2 [aw=w],a(county) cluster(county)

* Col 2
reghdfe dfinshare dfinshare2 [aw=w],a(county division#quarter) cluster(county)

* Col 3
reghdfe dfinshare dfinshare2 $control2 [aw=w],a(county division#quarter) cluster(county)

* Col 5
ivreghdfe dfinshare (dfinshare2=dfinshare2_outcz) $control2 [aw=w], a(division#quarter county) cluster(county) 

* Col 6
ivreghdfe dfinshare (dfinshare2=dfinshare2_outdis100) $control2 [aw=w], a(division#quarter county) cluster(county)

* Col 7
ivreghdfe dfinshare (dfinshare2=dfinshare2_outdis200) $control2 [aw=w], a(division#quarter county) cluster(county)  


*===============================================================================
* Table 2. Refi, purchase, volume
*===============================================================================

use beforereg_sci_iv,clear 

* Col 1
ivreghdfe dfinshare (dfinshare2=dfinshare2_outcz) $control2 [aw=w],a(county division#quarter) cluster(county)

* Col 2
ivreghdfe dfinshare_refi (dfinshare2=dfinshare2_outcz) $control2 [aw=w],a(county division#quarter) cluster(county) 

* Col 3
ivreghdfe dfinshare_pur (dfinshare2=dfinshare2_outcz) $control2 [aw=w],a(county division#quarter) cluster(county)

* Col 4
ivreghdfe dfin (dfinshare2=dfinshare2_outcz) $control2 [aw=w],a(county division#quarter) cluster(county)

* Col 5
ivreghdfe dnonfin (dfinshare2=dfinshare2_outcz) $control2 [aw=w],a(county division#quarter) cluster(county)

* Col 6
ivreghdfe dfin_refi (dfinshare2=dfinshare2_outcz) $control2 [aw=w],a(county division#quarter) cluster(county)

* Col 7
ivreghdfe dfin_pur (dfinshare2=dfinshare2_outcz) $control2 [aw=w],a(county division#quarter) cluster(county)



* ==============================================================================
* Table 4. Heterogeneity 
* ==============================================================================

* Col 1: By metro-pop
use beforereg_sci_iv,clear

* USDA rural-urban Continuum Code
merge m:1 county using Econdata\urban_rural
drop if _merge==2  
drop _merge 

gen urban_class=4 if urban==1   // metro pop>=1mil
replace urban_class=3 if inlist(urban,2,3)  // metro pop<1mil
replace urban_class=2 if inlist(urban,4,5,6,7)  // non-metro: urban pop>2.5K
replace urban_class=1 if urban==8|urban==9  // rural or urban pop<2.5k


foreach i of numlist 2/4{	
    gen X`i'_x  = (urban_class==`i')*dfinshare2
	gen X`i'_iv = (urban_class==`i')*dfinshare2_outcz
}
 
ivreghdfe dfinshare (dfinshare2 X*_x =dfinshare2_outcz X*_iv) $control2 [aw=w],a(division#quarter county) cluster(county) 


* Col 2: By education
use beforereg_sci_iv,clear

sum babove,d
gen edu=(babove>r(p50))
replace edu=. if babove==.

gen X_x =edu*dfinshare2
gen X_iv = edu*dfinshare2_outcz

ivreghdfe dfinshare (dfinshare2  X_x=dfinshare2_outcz X_iv) $control2 [aw=w] ,a(division#quarter county) cluster(county) 


* Col 3: By race
use beforereg_sci_iv,clear

sum fracb,d
gen black=(fracb>r(p50))
replace black=. if fracb==.

gen X_x =black*dfinshare2
gen X_iv = black*dfinshare2_outcz

ivreghdfe dfinshare (dfinshare2 X_x=dfinshare2_outcz X_iv) $control2 [aw=w] ,a(division#quarter county) cluster(county) 



*===============================================================================
* Table 5. Effect by lender segment
*===============================================================================

use beforereg_sci_iv,clear

* RHS: convert unit to 1sd
foreach x of varlist share_refi2 finshare_refi2 nonbshare_refi2 bankshare_refi2{
sum `x'
gen sd`x' =d`x'/r(sd)
}

* LHS: convert the unit to % of unconditional mean
foreach x of varlist share_refi finshare_refi nonbshare_refi bankshare_refi{
sum `x'
gen sd`x' = 100*d`x'/r(mean)
}

* Col 1
ivreghdfe sdshare_refi (sdshare_refi2=dshare_refi2_outcz) $control2 [aw=w], a(division#quarter county) cluster(county) 

* Col 2
ivreghdfe sdfinshare_refi (sdfinshare_refi2=dfinshare_refi2_outcz) $control2 [aw=w], a(division#quarter county) cluster(county) 

* Col 3
ivreghdfe sdnonbshare_refi (sdnonbshare_refi2=dnonbshare_refi2_outcz) $control2 [aw=w], a(division#quarter county) cluster(county)  

* Col 4
ivreghdfe sdbankshare_refi (sdbankshare_refi2=dbankshare_refi2_outcz) $control2 [aw=w], a(division#quarter county) cluster(county)  

